<template>
    <el-form :model="pwdData" :rules="rules" ref="formRef" label-width="120px" class="password-form">
      <el-form-item label="旧密码" prop="oldPwd">
        <el-input
          v-model="pwdData.oldPwd"
          type="password"
          placeholder="请输入旧密码"
          size="large"
          class="input-field"
        />
      </el-form-item>
  
      <el-form-item label="新密码" prop="newPwd">
        <el-input
          v-model="pwdData.newPwd"
          type="password"
          placeholder="请输入新密码"
          size="large"
          class="input-field"
        />
      </el-form-item>
  
      <el-form-item label="确认密码" prop="confirmPassword">
        <el-input
          v-model="pwdData.confirmPassword"
          type="password"
          placeholder="请再次输入新密码"
          size="large"
          class="input-field"
        />
      </el-form-item>
  
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" size="large" @click="updatePassword" class="submit-btn">确定</el-button>
      </div>
    </el-form>
  </template>
  
  <script setup>
  import { ref } from 'vue'
  import { ElForm, ElFormItem, ElInput, ElButton, ElMessage } from 'element-plus'
  import { updatePwd } from '@/api/admin.js'
  import userInfoStore from '@/stores/userInfo.js'
  
  const userStore = userInfoStore()
  
  const pwdData = ref({
    oldPwd: '',
    newPwd: '',
    confirmPassword: '',
    username: userStore.info.username
  })
  
  // 提交更新密码
  const updatePassword = async () => {
    // 如果验证通过，调用 API 更新密码
    const result = await updatePwd(pwdData.value)
    if (result.code === 200) {
      ElMessage.success('修改成功')
      pwdData.value={
        oldPwd: '',
        newPwd: '',
        confirmPassword: '',
      }
    } else {
      ElMessage.error('修改失败')
    }
  }
  
  // 表单验证规则
  const rules = ref({
    oldPwd: [
      { required: true, message: '请输入旧密码', trigger: 'blur' },
      { min: 6, max: 20, message: '密码长度应在6到20个字符之间', trigger: 'blur' }
    ],
    newPwd: [
      { required: true, message: '请输入新密码', trigger: 'blur' },
      { min: 6, max: 20, message: '密码长度应在6到20个字符之间', trigger: 'blur' }
    ],
    confirmPassword: [
      { required: true, message: '请确认新密码', trigger: 'blur' },
      {
        validator: (rule, value, callback) => {
          if (value !== pwdData.value.newPwd) {
            callback(new Error('两次输入的密码不一致'))
          } else {
            callback()
          }
        },
        trigger: 'blur'
      }
    ]
  })
  </script>
  
  <style scoped>
  .password-form {
    width: 480px; /* 增大表单宽度 */
    margin: 50px auto;
    padding: 30px;
    background-color: #f9f9f9;
    border-radius: 10px;
    box-shadow: 0 6px 18px rgba(0, 0, 0, 0.1);
  }
  
  .el-form-item {
    margin-bottom: 24px; /* 增大表单项间距 */
  }
  
  .el-input, .el-button {
    border-radius: 8px; /* 增大圆角 */
  }
  
  .input-field {
    height: 44px; /* 增大输入框高度 */
    font-size: 16px;
    border: 1px solid #dcdfe6;
    background-color: #fff;
  }
  
  .dialog-footer {
    text-align: center;
  }
  
  .submit-btn {
    width: 100%;
    padding: 14px;
    font-size: 18px; /* 增大按钮字体大小 */
    height: 48px; /* 增大按钮高度 */
  }
  </style>